package eduware.dozent;

import java.util.HashMap;
import java.util.Vector;

import eduware.Program;
import eduware.util.Config;
import eduware.util.WritesConfig;

/**
 * A class that enables you to handle the tutor's mood.<br>
 * Singleton
 * 
 * @author Florian Bausch
 * 
 */
public class Mood implements WritesConfig {

	private static class Holder {
		public static final Mood INSTANCE = new Mood();
	}

	/**
	 * Get the Singleton instance of Mood.
	 * 
	 * @return The instance of Mood
	 */
	public static Mood getInstance() {
		return Holder.INSTANCE;
	}

	private Vector<MoodListener> listeners = new Vector<MoodListener>();

	/**
	 * Creates a Mood instance with a predefined MoodListener and standard
	 * indices.
	 * 
	 * @param listener
	 *            A MoodListener.
	 */
	private Mood() {
		Config.getInstance().registerImplementedInterface(this);

		Config c = Config.getInstance();
		MoodState.SLEEP.setIndex(c.getFloat("sleepIndex", 100.0f));
		MoodState.MOOD.setIndex(c.getFloat("moodIndex", 100.0f));
		MoodState.TOILET.setIndex(c.getFloat("toiletIndex", 100.0f));
		MoodState.HUNGER.setIndex(c.getFloat("hungerIndex", 100.0f));
		MoodState.THIRST.setIndex(c.getFloat("thirstIndex", 100.0f));
		// MoodState.SLEEP.setRate(c.getFloat("sleepIndexRate", 1.0f));
		// MoodState.MOOD.setRate(c.getFloat("moodIndexRate", 1.0f));
		// MoodState.TOILET.setRate(c.getFloat("toiletIndexRate", 1.0f));
		// MoodState.HUNGER.setRate(c.getFloat("hungerIndexRate", 1.0f));
		// MoodState.THIRST.setRate(c.getFloat("thirstIndexRate", 1.0f));
	}

	private int triggerCount = 0;

	/**
	 * Sends a trigger to all registered listeners.
	 */
	public synchronized void trigger() {
		for (MoodListener listener : this.listeners) {
			listener.triggerMoodChanges();
		}
		// Avoid call of uninitialised Program object
		if (triggerCount > 2)
			Program.getInstance().getMainWindow().updateCanvas();
		else
			triggerCount++;
		Program.getInstance().getMainWindow().getBar().repaint();
	}

	/**
	 * If an object shall be informed about changes in the tutor's mood you can
	 * implement the MoodListener interface and add the implementation to the
	 * instance of Mood.
	 * 
	 * @param listener
	 *            An implementation of a MoodListener.
	 */
	public synchronized void addMoodListener(MoodListener listener) {
		this.listeners.add(listener);
	}

	/**
	 * Writes values to a HashMap that stores configuration values.
	 * 
	 * @param config
	 *            The HashMap&lt;String, String&gt; where the configuration
	 *            shall be written to.
	 */
	public void writeConfig(HashMap<String, String> config) {
		config.put("sleepIndex", "" + MoodState.SLEEP.getIndex());
		config.put("moodIndex", "" + MoodState.MOOD.getIndex());
		config.put("toiletIndex", "" + MoodState.TOILET.getIndex());
		config.put("hungerIndex", "" + MoodState.HUNGER.getIndex());
		config.put("thirstIndex", "" + MoodState.THIRST.getIndex());
		config.put("sleepIndexRate", "" + MoodState.SLEEP.getRate());
		config.put("moodIndexRate", "" + MoodState.MOOD.getRate());
		config.put("toiletIndexRate", "" + MoodState.TOILET.getRate());
		config.put("hungerIndexRate", "" + MoodState.HUNGER.getRate());
		config.put("thirstIndexRate", "" + MoodState.THIRST.getRate());
	}
}
